package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.tianxin.workbao.workBao.Activity.BaseActivity;
import com.tianxin.workbao.workBao.Activity.StaffDetailActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.User;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.holder.EmptyHolder;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by hacker on 2017/6/19.
 */

public class UserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    List<User> mUsers;
    private Context mContext;
    private int columns;

    public UserAdapter(List<User> users, Context mContext) {
        this.mUsers = users;
        this.mContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.staff_list_item, parent, false);
            return new UserAdapterViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mUsers == null || mUsers.isEmpty()) {
            return TYPE_EMPTY;
        } else {
            return TYPE_DEFAULT;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof UserAdapterViewHolder) {
            final User user = mUsers.get(position);
            ((UserAdapterViewHolder) holder).bindViewData(user);
            ((UserAdapterViewHolder) holder).setViewListener(user);
        }
    }

    @Override
    public int getItemCount() {
        if (mUsers != null && mUsers.size() > 0) {
            return mUsers.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class UserAdapterViewHolder extends RecyclerView.ViewHolder {

        CircleImageView staff_header;
        TextView staff_name;
        TextView staff_phone;
        TextView staff_organization;

        public UserAdapterViewHolder(View itemView) {
            super(itemView);
            staff_header = (CircleImageView) itemView.findViewById(R.id.staff_header);
            staff_name = (TextView) itemView.findViewById(R.id.staff_name);
            staff_phone = (TextView) itemView.findViewById(R.id.staff_phone);
            staff_organization = (TextView) itemView.findViewById(R.id.staff_organization);
        }

        public void bindViewData(User user) {
            String headphoto = user.getHeadphoto();
            if (headphoto.length() > 3) {
                Glide.with(mContext).load(headphoto).into(staff_header);
            } else {
                staff_header.setImageResource(R.mipmap.app_icon_circle);
            }
            staff_name.setText(user.getName());
            staff_phone.setText(BaseActivity.formatPhoneNumber(user.getMobiletel()));
            staff_organization.setText(user.getPorgname() + " " + user.getOrgname());
        }

        public void setViewListener(final User user) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("userInfo", user);
                    bundle.putString("userID", user.getId().toString());
                    Bitmap bitmap;
                    try {
                        GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) staff_header.getDrawable();
                        if (bitmapDrawable != null) {
                            bitmap = bitmapDrawable.getBitmap();
                            bundle.putParcelable("userHeader", bitmap);
                        }
                    } catch (Exception e) {
                        Log.d(Constant.COM_TAG, e.getMessage());
                    }
                    Intent intent = new Intent(mContext, StaffDetailActivity.class);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });
        }
    }

}
